<template>
  <div class="my-container">
    <div class="header">
      <div v-if="!token" class="not-login">
        <div class="login-btn" @click="$router.push('/login')">
          <van-image class="avatar" :src="require('../../assets/mobile.png')" round fit="cover"></van-image>
          <div>登录 / 注册</div>
        </div>
      </div>
      <div v-else class="logined">
        <div class="profile">
          <div class="left">
            <div class="avatar">
              <router-link
                :to="{
                  name:'MyPage',
                  params:{
                    userId: userInfo.id
                  }
                }"
              >
                <van-image class="avatar" :src="userInfo.photo" round fit="cover"></van-image>
              </router-link>
            </div>
            <span>{{ userInfo.name }}</span>
          </div>
          <div class="right">
            <van-button
              size="mini"
              class="edit"
              :to="{
                name:'EditUserInfo'
              }"
            >编辑资料</van-button>
          </div>
        </div>
        <div class="data">
          <div class="data-item">
            <span class="num">{{ userInfo.art_count }}</span>
            <span class="text">头条</span>
          </div>
          <router-link
            :to="{
              name: 'Friends',
              params: {
                type: 'follow'
              }
            }"
            class="data-item"
          >
            <span class="num">{{ userInfo.follow_count }}</span>
            <span class="text">关注</span>
          </router-link>
          <router-link
            :to="{
              name: 'Friends',
              params: {
                type: 'fans'
              }
            }"
            class="data-item"
          >
            <span class="num">{{ userInfo.fans_count }}</span>
            <span class="text">粉丝</span>
          </router-link>
          <div class="data-item">
            <span class="num">{{ userInfo.like_count }}</span>
            <span class="text">获赞</span>
          </div>

        </div>
      </div>
    </div>

    <div class="grid">
      <van-grid :column-num="2">
        <van-grid-item class="gridItem">
          <i slot="icon" class="iconfont icon-shoucang"></i>
          <span slot="text">收藏</span>
        </van-grid-item>
        <van-grid-item class="gridItem">
          <i slot="icon" class="iconfont icon-lishi1"></i>
          <span slot="text">历史</span>
        </van-grid-item>
      </van-grid>
    </div>
    <van-cell-group class="cellGroup">
      <van-cell title="消息通知" is-link></van-cell>
      <van-cell title="小智同学" is-link class="item"></van-cell>
    </van-cell-group>
    <van-cell-group v-if="token">
      <van-cell title="退出登录" class="lgout" @click="lgout"></van-cell>
    </van-cell-group>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user.js'

export default {
  name: 'MyIndex',
  components: {
  },
  data() {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['token'])
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    lgout() {
      this.$store.commit('setUser', null)
    },
    async getUserInfo() {
      const { data } = await getUserInfo()
      this.$store.commit('setUserId', data.data.id)
      this.userInfo = data.data
      console.log(this.userInfo);
    }
  }
}
</script>

<style scope lang="less">
.my-container {
  .header {
    background: url('~@/assets/banner.png');
    background-size: cover;
    .not-login {
      height: 360px;
      display: flex;
      justify-content: center;
      align-items: center;
      .login-btn {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        font-size: 28px;
        color: #fff;
        .avatar {
          width: 132px;
          height: 132px;
          border: 1px solid #fff;
          margin-bottom: 10px;
        }
      }
    }
    .logined {
      height: 460px;
      .profile {
        height: 228px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 74px 28px 47px;
        .left {
          display: flex;
          justify-content: flex-start;
          align-items: center;
          .avatar {
            width: 132px;
            height: 132px;
            border: 1px solid #fff;
            border-radius: 50%;
          }
          span {
            font-size: 36px;
            color: #fff;
            margin-left: 30px;
          }
        }
        .right {
          .edit {
            display: inline-block;
            text-align: center;
            width: 120px;
            height: 35px;
            line-height: 35px;
            font-size: 20px;
            border-radius: 16px;
            background-color: #fff;
          }
        };
      }
      .data{
          height:122px;
          width: 100%;
          display: flex;
          .data-item {
            flex: 1;
            display: flex;
            flex-direction: column;
            justify-content: flex-start;
            align-items: center;
            color:#fff;
            .num {
              font-size: 36px;
              margin-bottom: 5px;
            }
            .text {
              font-size: 23px;
            }
          }
        }
    }
  }
  .grid {
    margin-bottom: 5px;
    .gridItem {
      i.iconfont {
        font-size: 45px;
        color: #ff9d1d;
      }
      span {
        font-size: 28px;
      }
      i.icon-shoucang {
        color: red;
      }
    }
  }
  .cellGroup {
    margin-bottom: 18px;
  }
  .lgout{
    text-align: center;
    color: #d86262;
    height: 100px;
    line-height: 100px;
    padding:0
  }
}
</style>
